package cn.ziyoufa.smart.smartschool.serialport;

import android.annotation.SuppressLint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author benjaminwan
 *数据转换工具
 */
public class MyFunc {
	//-------------------------------------------------------
	// 判断奇数或偶数，位运算，最后一位是1则为奇数，为0是偶数
	static public int isOdd(int num) {
		return num & 0x1;
	}

	//-------------------------------------------------------
	static public int HexToInt(String inHex)//Hex字符串转int
	{
		return Integer.parseInt(inHex, 16);
	}

	//-------------------------------------------------------
	static public byte HexToByte(String inHex)//Hex字符串转byte
	{
		return (byte) Integer.parseInt(inHex, 16);
	}

	//-------------------------------------------------------
	static public String Byte2Hex(Byte inByte)//1字节转2个Hex字符
	{
		return String.format("%02x", inByte).toUpperCase();
	}

	//-------------------------------------------------------
	static public String ByteArrToHex(byte[] inBytArr)//字节数组转转hex字符串
	{
		StringBuilder strBuilder = new StringBuilder();
		int j = inBytArr.length;
		for (int i = 0; i < j; i++) {
			strBuilder.append(Byte2Hex(inBytArr[i]));
			strBuilder.append(" ");
		}
		return strBuilder.toString();
	}

	//-------------------------------------------------------
	static public String ByteArrToHex(byte[] inBytArr, int offset, int byteCount)//字节数组转转hex字符串，可选长度
	{
		StringBuilder strBuilder = new StringBuilder();
		int j = byteCount;
		for (int i = offset; i < j; i++) {
			strBuilder.append(Byte2Hex(inBytArr[i]));
		}
		return strBuilder.toString();
	}

	//-------------------------------------------------------
	//转hex字符串转字节数组
	static public byte[] HexToByteArr(String inHex)//hex字符串转字节数组
	{
		int hexlen = inHex.length();
		byte[] result;
		if (isOdd(hexlen) == 1) {//奇数
			hexlen++;
			result = new byte[(hexlen / 2)];
			inHex = "0" + inHex;
		} else {//偶数
			result = new byte[(hexlen / 2)];
		}
		int j = 0;
		for (int i = 0; i < hexlen; i += 2) {
			result[j] = HexToByte(inHex.substring(i, i + 2));
			j++;
		}
		return result;
	}

	public static byte[] longToByteArray1(int paramInt, long paramLong) {
		return new byte[]{0, -86, -1, 85, (byte) paramInt, (byte) (int) (paramLong >> 16 & 0xFF), (byte) (int) (paramLong >> 8 & 0xFF), (byte) (int) (paramLong & 0xFF), 85};
	}

	public static String StringData() {
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		String mDay = String.valueOf(c.get(Calendar.DAY_OF_MONTH));// 获取当前月份的日期号码
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "天";
		} else if ("2".equals(mWay)) {
			mWay = "一";
		} else if ("3".equals(mWay)) {
			mWay = "二";
		} else if ("4".equals(mWay)) {
			mWay = "三";
		} else if ("5".equals(mWay)) {
			mWay = "四";
		} else if ("6".equals(mWay)) {
			mWay = "五";
		} else if ("7".equals(mWay)) {
			mWay = "六";
		}
		return mDay + "日" + "/星期" + mWay;

	}

	public static String getCurrentWeekNum(){
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "7";
		} else if ("2".equals(mWay)) {
			mWay = "1";
		} else if ("3".equals(mWay)) {
			mWay = "2";
		} else if ("4".equals(mWay)) {
			mWay = "3";
		} else if ("5".equals(mWay)) {
			mWay = "4";
		} else if ("6".equals(mWay)) {
			mWay = "5";
		} else if ("7".equals(mWay)) {
			mWay = "6";
		}
		return mWay;
	}

	/**
	 * 把android数字星期几转成标准数字星期几
	 * @param weekNum
	 * @return
	 */
	public static int getWeekNum2Num(String weekNum){
		int mWay = 0;
		if ("1".equals(weekNum)) {
			mWay = 7;
		} else if ("2".equals(weekNum)) {
			mWay = 1;
		} else if ("3".equals(weekNum)) {
			mWay = 2;
		} else if ("4".equals(weekNum)) {
			mWay = 3;
		} else if ("5".equals(weekNum)) {
			mWay = 4;
		} else if ("6".equals(weekNum)) {
			mWay = 5;
		} else if ("7".equals(weekNum)) {
			mWay = 6;
		}
		return mWay;
	}

	//获取当前星期几
	public static String getCurrentWeekString(){
		final Calendar c = Calendar.getInstance();
		c.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
		String mWay = String.valueOf(c.get(Calendar.DAY_OF_WEEK));
		if ("1".equals(mWay)) {
			mWay = "天";
		} else if ("2".equals(mWay)) {
			mWay = "一";
		} else if ("3".equals(mWay)) {
			mWay = "二";
		} else if ("4".equals(mWay)) {
			mWay = "三";
		} else if ("5".equals(mWay)) {
			mWay = "四";
		} else if ("6".equals(mWay)) {
			mWay = "五";
		} else if ("7".equals(mWay)) {
			mWay = "六";
		}
		return "星期"+mWay;
	}

	//获取当前时和分的数组
	public static int[] getCurrentHMIntArr(){
		Calendar c = Calendar.getInstance();
		int mHour = c.get(Calendar.HOUR_OF_DAY);//获取时
		int mMinute = c.get(Calendar.MINUTE);//获取分
		int[] HM = {mHour,mMinute};
		return HM;
	}

	public static String getCurrentYMDHMS(){
		Calendar calendar = Calendar.getInstance();
		//年
		int year = calendar.get(Calendar.YEAR);
		//月
		int month = calendar.get(Calendar.MONTH)+1;
		//日
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		//获取系统时间
		// 小时
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		//分钟
		int minute = calendar.get(Calendar.MINUTE);
		//秒
		int second = calendar.get(Calendar.SECOND);

		return year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second;

	}

	public static String getCurrentY_M_DHMS(){
		Calendar calendar = Calendar.getInstance();
		//年
		int year = calendar.get(Calendar.YEAR);
		//月
		int month = calendar.get(Calendar.MONTH)+1;
		//日
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		//获取系统时间
		// 小时
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		//分钟
		int minute = calendar.get(Calendar.MINUTE);
		//秒
		int second = calendar.get(Calendar.SECOND);

		return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;

	}


	//根据时间戳(不需要除1000)，获取当前时分
	@SuppressLint({"SimpleDateFormat"})
	public static String getTranslateHM(long paramLong)
	{
		return new SimpleDateFormat("HH:mm").format(Long.valueOf(paramLong));
	}
	/* * 将时间转换为时间戳 */
    //字符串转时间戳
    public static String getTime(String timeString){
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm");
        Date d;
        try{
            d = sdf.parse(timeString);
            long l = d.getTime();
            timeStamp = String.valueOf(l);
        } catch(ParseException e){
            e.printStackTrace();
        }
        return timeStamp;
    }


	public static long getTimeString2Long(String paramString)
	{
		String[] paramStrings = paramString.split(":");
		return (Integer.parseInt(paramStrings[0]) * 3600 + Integer.parseInt(paramStrings[1]) * 60 + Integer.parseInt(paramStrings[2])) * 1000L;//1000L就是1秒
	}

	public static String[] getTimeString2Strings(String paramString)
	{
		String[] paramStrings = paramString.split(":");
		return paramStrings;
	}
}
